package com.sgb.kjwl.view.ui.adapter.shopping;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import androidx.annotation.NonNull;
import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;


import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MDateUtil;
import com.swgk.core.util.StringUtil;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by WYJ on 2020/8/31.
 */
public class ShopTeamAdapter extends BaseRecycleAdapter<ShopTeamInfoEntity> {

    private String mChannelId;

    public ShopTeamAdapter(Context context, int layoutId, List<ShopTeamInfoEntity> datas) {
        super(context, layoutId, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, ShopTeamInfoEntity shopTeamInfoEntity) {
        holder.setBinding(BR.viewModel, shopTeamInfoEntity);
        switch (mChannelId) {
            case "teamPublish": //我的发布
                teamPublishInitView(holder, shopTeamInfoEntity);
                break;
            case "teamOffer": //工种报价
                teamOfferData(holder, shopTeamInfoEntity);
                break;
            case "teamAchieve": //施工业绩
                teamAchieveInfoView(holder, shopTeamInfoEntity);
                break;
            case "teamInfo": //工队简介
                teamTeamListView(holder, shopTeamInfoEntity);
                break;
            case "teamMember":  //工队人员
                //updateRecycler(holder.itemView.findViewById(R.id.recycler), shopTeamInfoEntity.getCertificates());
                teamMemberInitView(holder, shopTeamInfoEntity);
                break;
            case "teamEquipment":  //自有设备
                updateRecycler(holder.itemView.findViewById(R.id.recycler), shopTeamInfoEntity.getPicture());
                break;
            case "teamAlbum":  //工队相册
                teamAlbum(holder.itemView.findViewById(R.id.imurl), shopTeamInfoEntity);
                break;
            default:
                break;
        }
    }

    private void teamTeamListView(ItemViewHolder holder, ShopTeamInfoEntity entity) {
        holder.setBinding(com.sgb.kjwl.BR.viewModel, entity);
        ImageView iv_packer_avatar = holder.itemView.findViewById(R.id.iv_packer_avatar);
        TextView tvTime = holder.itemView.findViewById(R.id.tv_time);
        if (null != entity.getAvatarAddress() && !TextUtils.isEmpty(entity.getAvatarAddress())) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.packer_avatar)
                    .transforms(new CenterCrop(), new RoundedCorners(8));
            Glide.with(context).load(entity.getAvatarAddress()).apply(options).into(iv_packer_avatar);
        } else {
            Glide.with(context).load(R.mipmap.packer_avatar).into(iv_packer_avatar);
        }
        tvTime.setText(String.format("%s 发布", DateUtils.timestampToDate(entity.getCreateTime())));
        TextView name1 = holder.itemView.findViewById(R.id.tv_packers_title);
        name1.setText(entity.getTeamName()+"");
    }

    private void teamOfferData(ItemViewHolder holder, ShopTeamInfoEntity shopTeamInfoEntity) {
        TextView tvTime = holder.itemView.findViewById(R.id.tv_time);
        tvTime.setText(String.format("%s 发布", DateUtils.timestampToDate(shopTeamInfoEntity.getCreateTime())));
    }

    private void teamAlbum(ImageView imurl, ShopTeamInfoEntity shopTeamInfoEntity) {
        if (TextUtils.isEmpty(shopTeamInfoEntity.getUrl())) {
            Glide.with(context).load(R.mipmap.worker_avatar).into(imurl);
        } else {
            Glide.with(context).load(shopTeamInfoEntity.getUrl()).into(imurl);
        }
       /* imurl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(context, PhotoViewActivity.class);
                intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) shopTeamInfoEntity.getUrl());
                intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, 0);
                context.startActivity(intent);
            }
        });*/
    }

    private void teamAchieveInfoView(ItemViewHolder holder, ShopTeamInfoEntity shopTeamInfoEntity) {
        TextView tvTimeEnd = holder.itemView.findViewById(R.id.tv_team_end);
        tvTimeEnd.setText(MDateUtil.getStartAndEndDate1(shopTeamInfoEntity.getStartTime(), shopTeamInfoEntity.getEndTime()));
        TextView tvTime = holder.itemView.findViewById(R.id.tv_time);
        tvTime.setText(DateUtils.timestampToDate(shopTeamInfoEntity.getCreateTime()) + "发布");
    }

    private void teamMemberInitView(ItemViewHolder holder, ShopTeamInfoEntity shopTeamInfoEntity) {
        ImageView workerAvatar = holder.itemView.findViewById(R.id.iv_worker_avatar);
        if (null != shopTeamInfoEntity.getAvatarAddress() && !TextUtils.isEmpty(shopTeamInfoEntity.getAvatarAddress())) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.packer_avatar)
                    .transforms(new CenterCrop(), new RoundedCorners(8));
            Glide.with(context).load(shopTeamInfoEntity.getAvatarAddress()).apply(options).into(workerAvatar);
        } else {
            Glide.with(context).load(R.mipmap.worker_avatar).into(workerAvatar);
        }
        TextView TvWorkerInfo = holder.itemView.findViewById(R.id.workerInfo);
        TextView tvPhone = holder.itemView.findViewById(R.id.tv_phones);
        TvWorkerInfo.setText(String.format("%s岁  |  %s工龄", shopTeamInfoEntity.getAge(), shopTeamInfoEntity.getWorkingYear()));
        tvPhone.setText(StringUtil.getPhoneToName(shopTeamInfoEntity.getContactPhone()));
        TextView certificateName = holder.itemView.findViewById(R.id.certificate_list);
        if (shopTeamInfoEntity.getCertificateName() != null && shopTeamInfoEntity.getCertificateName().size() > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String name : shopTeamInfoEntity.getCertificateName()) {
                stringBuilder.append(name).append(",");
            }
            certificateName.setText(stringBuilder.substring(0, stringBuilder.length() - 1));
        } else {
            certificateName.setText("");
        }
       /* RelativeLayout relativeLayout = holder.itemView.findViewById(R.id.certification_layout);
        if (TextUtils.isEmpty(shopTeamInfoEntity.getIdentity())) {
            relativeLayout.setVisibility(View.GONE);
        } else {
            relativeLayout.setVisibility(View.VISIBLE);
        }*/
        TextView workerPosition = holder.itemView.findViewById(R.id.workerposition);
        if (TextUtils.isEmpty(shopTeamInfoEntity.getPosition())) {
            workerPosition.setVisibility(View.GONE);
        } else {
            workerPosition.setVisibility(View.VISIBLE);
        }
        //处理资政证明和健康证明
        /*ImageView certifications = holder.itemView.findViewById(R.id.certification);

       if (shopTeamInfoEntity.getCertificateName().size()>0){

           certifications.setVisibility(View.VISIBLE);
       }else {
           certifications.setVisibility(View.GONE);
       }*/

    }

    private void teamPublishInitView(ItemViewHolder holder, ShopTeamInfoEntity entity) {
        TextView tvTime = holder.itemView.findViewById(R.id.tv_time);
        TextView tvEndDate = holder.itemView.findViewById(R.id.tv_end_time);
        TextView tvPublishTime = holder.itemView.findViewById(R.id.tv_publish_time);

        tvEndDate.setText(DateUtils.timestampToDate(entity.getEndDate()));
        tvTime.setText(MDateUtil.getStartAndEndDate1(entity.getStartTime(), entity.getEndTime()));
        tvPublishTime.setText(DateUtils.timestampToDate(entity.getCreateTime()) + " 发布");
    }

    public void setChannelId(String channelId) {
        mChannelId = channelId;
    }

    private void updateRecycler(RecyclerView recycler, String picData) {
        if (!TextUtils.isEmpty(picData)) {
            recycler.setVisibility(View.VISIBLE);
            GridLayoutManager manager = new GridLayoutManager(recycler.getContext(), 4);
            recycler.setLayoutManager(manager);
            List<String> images = Arrays.asList(picData.split(","));
            recycler.setAdapter(new ShopTeamPicAdapter(recycler.getContext(), images, manager));
        } else {
            recycler.setVisibility(View.GONE);
        }
    }
}
